﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RaygunDiplomacy.Model.Terrain.Local;
using RaygunDiplomacy.ViewModel.Isometric;

namespace RaygunDiplomacy.ViewModel.IsoTerrain
{
   public class ChunkViewModel
   {
      Chunk chunk;
      SpatialIndex chunkIndex;
      CellViewModel[] visibleCellCache;

      IsoCamera camera;
      VisibilitySection currentSection;

      private void UpdateCache()
      {
         
      }

      public ChunkViewModel(Chunk chunk, SpatialIndex chunkIndex, IsoCamera camera)
      {
         this.chunk = chunk;
         this.camera = camera;
         this.chunkIndex = chunkIndex;

         VisibilitySection section = null;

         this.camera.PropertyChanged.Subscribe(name =>
         {
            if (name == IsoCamera.CurrentDirectionProperty)
            {
               UpdateCache();
            }
            else
            {
               var queryChunkIndex = camera.GetChunkIndex();
               var queryCellIndex = camera.GetCellIndex();

               if (this.chunkIndex == queryChunkIndex)
               {
                  section = chunk.GetVisibilitySection(queryCellIndex);
               }
               else
               {
                  section = chunk.FindConnected(queryChunkIndex, queryCellIndex);
               }

               if (section != currentSection)
               {
                  UpdateCache();
               }
            }
         });
      }
   }
}
